/*
*******************************************************************************
* ansi c source code
* file name:
*	Modulator_PN.c
* abstract:
*	to modulate the training symbol
* author:Fangming He
*******************************************************************************
*/

/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "globaldef.h"
#include "ReadFile.h"

/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
*	to modulate the first training symbol via Diff_QPSK 
*  input
*  length  : the length of PN data 
*  PN1  : to store the modulated PN 
*  referenced  :
*  ReadFile.c:  
* author:
*	guo liang       2006-05-31
* revision history:
*	guo liang       2006-05-31	original version
*******************************************************************************
*/

int  Modulator1( int length , struct Complex *PN1)
{
	int i;
	int *input;
	int length1;
	input = (int *) malloc (length * sizeof (int));
	if(input == NULL)
	{
		printf("error\n");
		return 0;
	
	}
	memset(input,0,length * sizeof (int));
    ReadData("PN1.txt",input,&length1);
    if ( length1 != length)
	{
		printf("error\n");
	}
	for (i = 0; i < length/2; i++)
	{
		 switch ( input[2*i] )
			{
		        case -1:
			        PN1[2*i].real =  1;
			        break;
		        case 1:
			        PN1[2*i].real = -1;
			        break;
		        default:
			        return 0;
			}
		    switch ( input[2*i + 1] )
			{
		        case -1:
			        PN1[2*i].imag =  1;
			        break;
		        case 1:
			        PN1[2*i].imag = -1;
			        break;
		        default:
			        return 0;
			}
	}
	free(input);
	return 1;
}
/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
*	to modulate the first training symbol via QPSK
*  input
*  length  : the length of PN data 
*  PN2  : to store the modulated PN 
*  referenced  :
*  ReadFile.c:  
* author:
*	guo liang       2006-05-31
* revision history:
*	guo liang       2006-05-31	original version
*******************************************************************************
*/
int  Modulator2( int length , struct Complex *PN2)
{
	int i;
	int *input;
	int length2;
	input = (int *) malloc (length * sizeof (int));
	if(input == NULL)
	{
		printf("error\n");
		return 0;
	
	}
	memset(input,0,length * sizeof (int));
    ReadData("PN2.txt",input,&length2);
    if ( length2 != length)
	{
		printf("error\n");
	}
	for (i = 0; i < length/2; i++)
	{
		if (input[2*i] == input[2*i+1])
		{
			PN2[2*i].imag = 0;
			PN2[2*i].real = -input[2*i];
		}
	    else
		{
			PN2[2*i].real = 0;
			PN2[2*i].imag = input[2*i];
		}
	}
	free(input);
	return 1;
}
